<template>
  <div class="bg-white shadow-md p-4">
    <Navigation :homeRoute="homeRoute" :currentPage="currentPage" />
  </div>
  <div class="workbench-container">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="logo">医生工作台</div>
      <div class="user-info">
        <span>欢迎你，{{ username }}!</span>
        <el-dropdown @command="handleLogout">
          <span class="avatar">👤</span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="settings">设置</el-dropdown-item>
              <el-dropdown-item command="logout">退出</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <!-- 主体内容 -->
    <div class="main-content">
      <!-- 就诊数据统计区域 -->
      <div class="echarts-chart-container">
        <div ref="chartContainer" style="width: 100%; height: 400px;"></div>
      </div>

      <!-- 数据卡片区域 -->
      <el-row class="stat-row" :gutter="20">
        <el-col :span="8">
          <el-card class="stat-card">
            <div class="stat-title">今日就诊人数</div>
            <div class="stat-value">{{ todayVisits }} 人</div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="stat-card">
            <div class="stat-title">待处理检查</div>
            <div class="stat-value">{{ pendingExams }} 项</div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="stat-card">
            <div class="stat-title">未发药品</div>
            <div class="stat-value">{{ unpaidDrugs }} 单</div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 最近操作记录 -->
      <el-card class="operation-log">
        <div class="card-header">最近操作记录</div>
        <el-table :data="operationLogs" stripe size="small">
          <el-table-column prop="time" label="时间" width="180"></el-table-column>
          <el-table-column prop="action" label="操作内容" min-width="300"></el-table-column>
          <el-table-column prop="status" label="状态" width="100">
            <template #default="scope">
              <el-tag :type="scope.row.status === 'success' ? 'success' : 'warning'">
                {{ scope.row.status === 'success' ? '完成' : '进行中' }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, getCurrentInstance } from 'vue';
import { ElMessage } from 'element-plus';
import Navigation from '@/components/Navigation.vue';
const instance = getCurrentInstance();
const chartContainer = ref(null);
const currentPage = '工作台';
// 模拟数据
const username = ref('雷医生');
const todayVisits = ref(12);
const pendingExams = ref(5);
const unpaidDrugs = ref(3);
const operationLogs = ref([
  { time: '2025-05-30 14:30', action: '开具血常规检查', status: 'success' },
  { time: '2025-05-30 14:15', action: '保存患者病历', status: 'success' },
  { time: '2025-05-30 13:40', action: '新增就诊记录', status: '进行中' }
]);

// 退出登录
const handleLogout = (command) => {
  if (command === 'logout') {
    ElMessage.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      // 执行退出逻辑
      ElMessage.success('退出成功');
    });
  }
};

onMounted(() => {
  // 使用全局注册的 $echarts
  const echarts = instance.appContext.config.globalProperties.$echarts;
  
  // 初始化 ECharts 图表
  const chart = echarts.init(chartContainer.value);
  const option = {
    title: {
      text: '就诊数据统计',
      subtext: '近一周就诊情况',
      left: 'center'
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        name: '就诊分类',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 30, name: '普通门诊' },
          { value: 15, name: '专家门诊' },
          { value: 10, name: '急诊' },
          { value: 5, name: '复诊' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  };
  
  chart.setOption(option);
  
  // 监听窗口大小变化，自适应图表
  window.addEventListener('resize', () => {
    chart.resize();
  });
});
</script>

<style scoped>
.workbench-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background: #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
  font-size: 14px;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.main-content {
  flex: 1;
  padding: 20px;
  background: #f0f2f5;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.echarts-chart-container {
  width: 100%;
  height: 400px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.stat-row {
  margin-bottom: 20px;
}

.stat-card {
  height: 120px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.stat-title {
  font-size: 14px;
  color: #999;
  margin-bottom: 5px;
}

.stat-value {
  font-size: 28px;
  font-weight: 500;
  color: #333;
}

.operation-log {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  padding: 20px;
}

.card-header {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 10px;
}

.bg-white {
    background-color: #fff;
  }
  .shadow-md {
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  }
  .p-4 {
    padding: 1rem;
  }
</style>